using Microsoft.Phone.Controls;
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
namespace MicroMsg.UI.UserContrl
{
	public class PostPreViewImage : UserControl
	{
		private const double screenW = 480.0;
		private const double screenH = 750.0;
		private RotateTransform rotateTransform_0;
		private bool isEnableDrag = true;
		private double initialAngle;
		private double initialScale;
		private double initialX;
		private double initialY;
		private double maxRadio = 5.0;
		internal Grid LayoutRoot;
		internal Image ShowImg;
		internal CompositeTransform transform;
		internal GestureListener borderListener;
		private bool _contentLoaded;
		public PostPreViewImage()
		{
			this.InitializeComponent();
			this.rotateTransform_0 = new RotateTransform();
		}
		public void InitView(ImageSource imgSrc, int nAngle = 0)
		{
			this.rotateTransform_0.set_Angle((double)nAngle);
			this.ShowImg.set_RenderTransform(this.rotateTransform_0);
			this.ShowImg.set_RenderTransformOrigin(new Point(0.5, 0.5));
			this.ShowImg.set_Source(imgSrc);
		}
		public void ClearImage()
		{
			BitmapImage bitmapImage = this.ShowImg.get_Source() as BitmapImage;
			if (bitmapImage == null)
			{
				return;
			}
			bitmapImage.set_UriSource(null);
			this.ShowImg.set_Source(null);
		}
		private void OnPinchStarted(object sender, PinchStartedGestureEventArgs e)
		{
			if (!this.isEnableDrag)
			{
				return;
			}
			this.ShowImg.set_RenderTransform(this.transform);
			this.initialScale = this.transform.get_ScaleX();
		}
		private CompositeTransform GetCopyTransform(CompositeTransform trans)
		{
			CompositeTransform compositeTransform = new CompositeTransform();
			compositeTransform.set_CenterX(trans.get_CenterX());
			compositeTransform.set_CenterY(trans.get_CenterY());
			compositeTransform.set_ScaleX(trans.get_ScaleX());
			compositeTransform.set_ScaleY(trans.get_ScaleY());
			compositeTransform.set_TranslateX(trans.get_TranslateX());
			compositeTransform.set_TranslateY(trans.get_TranslateY());
			return compositeTransform;
		}
		private CompositeTransform GetContraryTransform(CompositeTransform trans)
		{
			CompositeTransform compositeTransform = new CompositeTransform();
			compositeTransform.set_CenterX(0.0);
			compositeTransform.set_CenterY(0.0);
			compositeTransform.set_ScaleX(1.0 / trans.get_ScaleX());
			compositeTransform.set_ScaleY(1.0 / trans.get_ScaleY());
			compositeTransform.set_TranslateX(0.0 - trans.get_TranslateX());
			compositeTransform.set_TranslateY(0.0 - trans.get_TranslateY());
			return compositeTransform;
		}
		private void OnPinchDelta(object sender, PinchGestureEventArgs e)
		{
			if (!this.isEnableDrag)
			{
				return;
			}
			double num = e.DistanceRatio;
			if (num > 2.0)
			{
				num = 2.0;
			}
			else
			{
				if (num < 0.3)
				{
					num = 0.3;
				}
			}
			if (this.initialScale * num > 0.65 && this.initialScale * num < this.maxRadio)
			{
				Point resultPoint = new Point(e.GetPosition(this.ShowImg).get_X(), e.GetPosition(this.ShowImg).get_Y());
				Point pointSrc = default(Point);
				bool oldPoint = this.GetOldPoint(resultPoint, this.transform, ref pointSrc);
				CompositeTransform copyTransform = this.GetCopyTransform(this.transform);
				copyTransform.set_ScaleX(this.initialScale * num);
				copyTransform.set_ScaleY(this.initialScale * num);
				Point point = default(Point);
				bool oldPoint2 = this.GetOldPoint(resultPoint, copyTransform, ref point);
				Point point2 = default(Point);
				bool transformPoint = this.GetTransformPoint(pointSrc, copyTransform, ref point2);
				this.transform.set_ScaleX(this.initialScale * num);
				this.transform.set_ScaleY(this.initialScale * num);
				if (oldPoint && oldPoint2 && transformPoint)
				{
					CompositeTransform expr_148 = this.transform;
					expr_148.set_TranslateX(expr_148.get_TranslateX() + (resultPoint.get_X() - point2.get_X()));
					CompositeTransform expr_169 = this.transform;
					expr_169.set_TranslateY(expr_169.get_TranslateY() + (resultPoint.get_Y() - point2.get_Y()));
				}
				return;
			}
		}
		private void GestureListener_DragStarted(object sender, DragStartedGestureEventArgs e)
		{
			if (!this.isEnableDrag)
			{
				return;
			}
			this.initialX = this.transform.get_TranslateX();
			this.initialY = this.transform.get_TranslateY();
		}
		private void GestureListener_DragDelta(object sender, DragDeltaGestureEventArgs e)
		{
			if (!this.isEnableDrag)
			{
				return;
			}
			if (this.transform.get_ScaleX() == 1.0)
			{
				return;
			}
			CompositeTransform expr_26 = this.transform;
			expr_26.set_TranslateX(expr_26.get_TranslateX() + e.HorizontalChange * 1.2);
			CompositeTransform expr_48 = this.transform;
			expr_48.set_TranslateY(expr_48.get_TranslateY() + e.VerticalChange * 1.2);
		}
		private void GestureListener_DragCompleted(object sender, DragCompletedGestureEventArgs e)
		{
			if (!this.isEnableDrag)
			{
				return;
			}
			this.AdjustImagePosition();
		}
		private void AdjustImagePosition()
		{
			double num = 0.0;
			double num2 = 0.0;
			if (!this.IsShouldReplace(ref num, ref num2))
			{
				return;
			}
			TimeSpan timeSpan = TimeSpan.FromSeconds(0.3);
			Storyboard storyboard = new Storyboard();
			double num3 = this.transform.get_TranslateX();
			double num4 = this.transform.get_TranslateX() + num;
			DoubleAnimation doubleAnimation = new DoubleAnimation();
			doubleAnimation.set_From(new double?(num3));
			doubleAnimation.set_To(new double?(num4));
			doubleAnimation.set_Duration(timeSpan);
			DoubleAnimation doubleAnimation2 = doubleAnimation;
			Storyboard.SetTarget(doubleAnimation2, this.transform);
			Storyboard.SetTargetProperty(doubleAnimation2, new PropertyPath(CompositeTransform.TranslateXProperty));
			storyboard.get_Children().Add(doubleAnimation2);
			num3 = this.transform.get_TranslateY();
			num4 = this.transform.get_TranslateY() + num2;
			DoubleAnimation doubleAnimation3 = new DoubleAnimation();
			doubleAnimation3.set_From(new double?(num3));
			doubleAnimation3.set_To(new double?(num4));
			doubleAnimation3.set_Duration(timeSpan);
			DoubleAnimation doubleAnimation4 = doubleAnimation3;
			Storyboard.SetTarget(doubleAnimation4, this.transform);
			Storyboard.SetTargetProperty(doubleAnimation4, new PropertyPath(CompositeTransform.TranslateYProperty));
			storyboard.get_Children().Add(doubleAnimation4);
			storyboard.Begin();
		}
		private void ResumeStoryBoard()
		{
			TimeSpan timeSpan = TimeSpan.FromSeconds(0.3);
			Storyboard storyboard = new Storyboard();
			double num = this.transform.get_ScaleX();
			double num2 = 1.0;
			DoubleAnimation doubleAnimation = new DoubleAnimation();
			doubleAnimation.set_From(new double?(num));
			doubleAnimation.set_To(new double?(num2));
			doubleAnimation.set_Duration(timeSpan);
			DoubleAnimation doubleAnimation2 = doubleAnimation;
			Storyboard.SetTarget(doubleAnimation2, this.transform);
			Storyboard.SetTargetProperty(doubleAnimation2, new PropertyPath(CompositeTransform.ScaleXProperty));
			storyboard.get_Children().Add(doubleAnimation2);
			num = this.transform.get_ScaleY();
			num2 = 1.0;
			DoubleAnimation doubleAnimation3 = new DoubleAnimation();
			doubleAnimation3.set_From(new double?(num));
			doubleAnimation3.set_To(new double?(num2));
			doubleAnimation3.set_Duration(timeSpan);
			DoubleAnimation doubleAnimation4 = doubleAnimation3;
			Storyboard.SetTarget(doubleAnimation4, this.transform);
			Storyboard.SetTargetProperty(doubleAnimation4, new PropertyPath(CompositeTransform.ScaleYProperty));
			storyboard.get_Children().Add(doubleAnimation4);
			num = this.transform.get_TranslateX();
			num2 = 0.0;
			DoubleAnimation doubleAnimation5 = new DoubleAnimation();
			doubleAnimation5.set_From(new double?(num));
			doubleAnimation5.set_To(new double?(num2));
			doubleAnimation5.set_Duration(timeSpan);
			DoubleAnimation doubleAnimation6 = doubleAnimation5;
			Storyboard.SetTarget(doubleAnimation6, this.transform);
			Storyboard.SetTargetProperty(doubleAnimation6, new PropertyPath(CompositeTransform.TranslateXProperty));
			storyboard.get_Children().Add(doubleAnimation6);
			num = this.transform.get_TranslateY();
			num2 = 0.0;
			DoubleAnimation doubleAnimation7 = new DoubleAnimation();
			doubleAnimation7.set_From(new double?(num));
			doubleAnimation7.set_To(new double?(num2));
			doubleAnimation7.set_Duration(timeSpan);
			DoubleAnimation doubleAnimation8 = doubleAnimation7;
			Storyboard.SetTarget(doubleAnimation8, this.transform);
			Storyboard.SetTargetProperty(doubleAnimation8, new PropertyPath(CompositeTransform.TranslateYProperty));
			storyboard.get_Children().Add(doubleAnimation8);
			num = this.transform.get_CenterX();
			num2 = 0.0;
			DoubleAnimation doubleAnimation9 = new DoubleAnimation();
			doubleAnimation9.set_From(new double?(num));
			doubleAnimation9.set_To(new double?(num2));
			doubleAnimation9.set_Duration(timeSpan);
			DoubleAnimation doubleAnimation10 = doubleAnimation9;
			Storyboard.SetTarget(doubleAnimation10, this.transform);
			Storyboard.SetTargetProperty(doubleAnimation10, new PropertyPath(CompositeTransform.CenterXProperty));
			storyboard.get_Children().Add(doubleAnimation10);
			num = this.transform.get_CenterY();
			num2 = 0.0;
			DoubleAnimation doubleAnimation11 = new DoubleAnimation();
			doubleAnimation11.set_From(new double?(num));
			doubleAnimation11.set_To(new double?(num2));
			doubleAnimation11.set_Duration(timeSpan);
			DoubleAnimation doubleAnimation12 = doubleAnimation11;
			Storyboard.SetTarget(doubleAnimation12, this.transform);
			Storyboard.SetTargetProperty(doubleAnimation12, new PropertyPath(CompositeTransform.CenterYProperty));
			storyboard.get_Children().Add(doubleAnimation12);
			storyboard.Begin();
		}
		private Rect GetOriginRect(ref double xRadio, ref double yRadio)
		{
			Rect result = default(Rect);
			BitmapImage bitmapImage = this.ShowImg.get_Source() as BitmapImage;
			if (bitmapImage == null)
			{
				return result;
			}
			double num;
			double num2;
			double num5;
			double num6;
			if ((double)(bitmapImage.get_PixelHeight() / bitmapImage.get_PixelWidth()) > 1.5625)
			{
				num = 0.0;
				num2 = 750.0;
				double num3 = 750.0 / (double)bitmapImage.get_PixelHeight();
				double num4 = (double)bitmapImage.get_PixelWidth() * num3;
				num5 = (480.0 - num4) / 2.0;
				num6 = num5 + num4;
			}
			else
			{
				num5 = 0.0;
				num6 = 480.0;
				double num7 = 480.0 / (double)bitmapImage.get_PixelWidth();
				double num8 = (double)bitmapImage.get_PixelHeight() * num7;
				num = (750.0 - num8) / 2.0;
				num2 = 750.0 - num;
			}
			xRadio = (num6 - num5) / 480.0;
			yRadio = (num2 - num) / 750.0;
			result = new Rect(num5, num, num6 - num5, num2 - num);
			return result;
		}
		private bool IsShouldReplace(ref double detaX, ref double detaY)
		{
			Rect transformRect = this.GetTransformRect(this.transform);
			double num = 0.0;
			double num2 = 0.0;
			this.GetOriginRect(ref num, ref num2);
			if (transformRect.get_IsEmpty())
			{
				return false;
			}
			double num3;
			double num4;
			if (transformRect.get_Width() < 480.0)
			{
				num3 = (480.0 - transformRect.get_Width()) / 2.0;
				num4 = num3 + transformRect.get_Width();
			}
			else
			{
				num3 = 0.0;
				num4 = 480.0;
			}
			bool result = false;
			if (transformRect.get_Width() < 480.0)
			{
				if (transformRect.get_Left() < num3)
				{
					detaX = num3 - transformRect.get_Left();
					result = true;
				}
				else
				{
					if (transformRect.get_Right() > num4)
					{
						detaX = num4 - transformRect.get_Right();
						result = true;
					}
				}
			}
			else
			{
				if (transformRect.get_Left() > 0.0 && transformRect.get_Right() > 480.0)
				{
					detaX = 0.0 - transformRect.get_Left();
					if (detaX < 0.0)
					{
						result = true;
					}
					else
					{
						detaX = 0.0;
					}
				}
				else
				{
					if (transformRect.get_Left() < 0.0 && transformRect.get_Right() < 480.0)
					{
						detaX = 480.0 - transformRect.get_Right();
						if (detaX > 0.0)
						{
							result = true;
						}
						else
						{
							detaX = 0.0;
						}
					}
				}
			}
			double num5;
			double num6;
			if (transformRect.get_Height() < 750.0)
			{
				num5 = (750.0 - transformRect.get_Height()) / 2.0;
				num6 = num5 + transformRect.get_Height();
			}
			else
			{
				num5 = 0.0;
				num6 = 750.0;
			}
			if (transformRect.get_Height() < 750.0)
			{
				if (transformRect.get_Top() > num5)
				{
					detaY = num5 - transformRect.get_Top();
					result = true;
				}
				else
				{
					if (transformRect.get_Bottom() < num6)
					{
						detaY = num6 - transformRect.get_Bottom();
						result = true;
					}
				}
			}
			else
			{
				if (transformRect.get_Top() > 0.0 && transformRect.get_Bottom() > 750.0)
				{
					detaY = num5 - transformRect.get_Top();
					if (detaY < 0.0)
					{
						result = true;
					}
					else
					{
						detaY = 0.0;
					}
				}
				else
				{
					if (transformRect.get_Top() < 0.0 && transformRect.get_Bottom() < 750.0)
					{
						detaY = num6 - transformRect.get_Bottom();
						if (detaY >= 0.0)
						{
							result = true;
						}
						else
						{
							detaY = 0.0;
						}
					}
				}
			}
			return result;
		}
		private bool GetTransformPoint(Point pointSrc, CompositeTransform trans, ref Point resultPoint)
		{
			double num = 0.0;
			double num2 = 0.0;
			Rect originRect = this.GetOriginRect(ref num, ref num2);
			if (originRect.get_IsEmpty())
			{
				return false;
			}
			if (pointSrc.get_X() < originRect.get_Left() || pointSrc.get_X() > originRect.get_Right())
			{
				return false;
			}
			if (pointSrc.get_Y() >= originRect.get_Top() && pointSrc.get_Y() <= originRect.get_Bottom())
			{
				double num3 = (pointSrc.get_X() - originRect.get_Left()) / originRect.get_Width();
				double num4 = (pointSrc.get_Y() - originRect.get_Top()) / originRect.get_Height();
				Rect transformRect = this.GetTransformRect(trans);
				resultPoint = new Point(transformRect.get_Left() + transformRect.get_Width() * num3, transformRect.get_Top() + transformRect.get_Height() * num4);
				return true;
			}
			return false;
		}
		private bool GetOldPoint(Point resultPoint, CompositeTransform trans, ref Point pointSrc)
		{
			double num = 0.0;
			double num2 = 0.0;
			Rect originRect = this.GetOriginRect(ref num, ref num2);
			if (originRect.get_IsEmpty())
			{
				return false;
			}
			Rect transformRect = this.GetTransformRect(trans);
			if (resultPoint.get_X() < transformRect.get_Left() || resultPoint.get_X() > transformRect.get_Right())
			{
				return false;
			}
			if (resultPoint.get_Y() >= transformRect.get_Top() && resultPoint.get_Y() <= transformRect.get_Bottom())
			{
				double num3 = (resultPoint.get_X() - transformRect.get_Left()) / transformRect.get_Width();
				double num4 = (resultPoint.get_Y() - transformRect.get_Top()) / transformRect.get_Height();
				pointSrc = new Point(originRect.get_Left() + originRect.get_Width() * num3, originRect.get_Top() + originRect.get_Height() * num4);
				return true;
			}
			return false;
		}
		private Rect GetTransformRect(CompositeTransform trans)
		{
			double num = 0.0;
			double num2 = 0.0;
			Rect originRect = this.GetOriginRect(ref num, ref num2);
			if (originRect.get_IsEmpty())
			{
				return originRect;
			}
			Rect rect = new Rect(0.0, 0.0, originRect.get_Width(), originRect.get_Height());
			Rect rect2 = trans.TransformBounds(rect);
			Rect result = new Rect(originRect.get_Left() + rect2.get_Left(), originRect.get_Top() + rect2.get_Top(), rect2.get_Width(), rect2.get_Height());
			return result;
		}
		private void GestureListener_PinchCompleted(object sender, PinchGestureEventArgs e)
		{
			if (this.transform.get_ScaleX() < 1.0)
			{
				this.ResumeStoryBoard();
				return;
			}
			this.AdjustImagePosition();
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/Sns/Common/UI/UploadAndPost/PostPreViewImage.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.ShowImg = (Image)base.FindName("ShowImg");
			this.transform = (CompositeTransform)base.FindName("transform");
			this.borderListener = (GestureListener)base.FindName("borderListener");
		}
	}
}
